package com.itheima.app.manager;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.itheima.domain.db.Announcement;
import com.itheima.domain.vo.AnnouncementVo;
import com.itheima.domain.vo.PageBeanVo;
import com.itheima.service.db.AnnouncementService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
public class AnnouncementManager {


    @DubboReference
    private AnnouncementService announcementService;

    public PageBeanVo announcements(Integer page, Integer pagesize) {
        PageBeanVo pageBeanVo = announcementService.findByPage(page, pagesize);
        List<Announcement> announcementList = (List<Announcement>) pageBeanVo.getItems();

        //System.out.println("公告数据:"+announcementList);

        List<AnnouncementVo> announcementVoList = new ArrayList<>();

        if(CollectionUtil.isNotEmpty(announcementList)){
            for (Announcement announcement : announcementList) {

                //别new在外面 呆瓜
                AnnouncementVo announcementVo = new AnnouncementVo();

                Date created = announcement.getCreated();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                String strCreated = sdf.format(created);

                //将announcement中数据 复制到 announcement中 主要将Date类型转换成String类型
                BeanUtil.copyProperties(announcement,announcementVo);

                //其他数据直接覆盖 日期转换成String类型
                announcementVo.setCreateDate(strCreated);
                announcementVoList.add(announcementVo);

                //System.out.println("每加入一个对象就打印看一次给我看:"+announcementVoList);
            }
        }

        pageBeanVo.setItems(announcementVoList);
        return pageBeanVo;
    }
}
